package com.codegero.android.client.sms.gateway.boundary;

import java.util.List;

import com.codegero.android.client.sms.gateway.R;
import com.codegero.android.client.sms.gateway.entity.Vote;
import com.codegero.android.client.sms.gateway.stored.DatabaseManager;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DaftarVote extends ListActivity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.general_list);
		Intent intent 	= getIntent();
		String idVote 	= intent.getStringExtra("id");
		String nameVote	= intent.getStringExtra("name");
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(nameVote);
		databaseManager = new DatabaseManager(this);
		votes = databaseManager.getVotesById(idVote);
		
		getListView().setAdapter
			(new AdapterDaftarVote
					(this, R.layout.general_item, votes));
		ImageView button = (ImageView) findViewById(R.id.buttonGeneral);
		button.setOnClickListener(this);
	}
	
	private class AdapterDaftarVote extends ArrayAdapter<Vote>{

		public AdapterDaftarVote(Context context,
				int textViewResourceId, List<Vote> objects) {
			super(context, textViewResourceId, objects);
			this.context 	= context;
			this.resource 	= textViewResourceId;
			votes = objects;
			
		}
		
		@Override
		public int getCount() {
			return votes!=null?votes.size():0;
		}
		

		@Override
		public Vote getItem(int position) {
			return votes.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			Vote vote = getItem(position);
			
			if(vote == null) return null;
			
			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			View rowView = inflater.inflate(resource, parent, false);
			
			TextView noHp 		= (TextView) rowView.findViewById(R.id.generalId);
			TextView message	= (TextView) rowView.findViewById(R.id.generalMessage);
			
			noHp.setText(vote.getNoHp());
			message.setText(vote.getMessage());
			
			if(position%2==0){
				rowView.setBackgroundColor(0xCCFFCC);
			}else{
				rowView.setBackgroundColor(0xFFFFFF);
			}
			
			return rowView;
		}
		
		private List<Vote> votes ;
		private final Context context;
		private final int resource;
		
	}
	
	

	@Override
	protected Dialog onCreateDialog(int id) {
		//TODO :  create dialog open it!!! 
		switch(id){
			case VOTE_RESULT:{
				return null;
			}
		}
		return super.onCreateDialog(id);
	}
	
	public void onClick(View v) {
		showDialog(VOTE_RESULT);
	}


	private List<Vote> votes;
	private DatabaseManager databaseManager;
	
	private static final int VOTE_RESULT = 0;
	
	
}
